/*	=======================================================================	*/
/// <remarks>
/// Version:
///		- number:	0.0.7
///		- build:	20080614
///		- status:	beta
/// </remarks>
/*	=======================================================================	*/
/// <remarks>
/// Global Variables
/// </remarks>
	var x_frm = new ClassX_Form(x_debug);
	var x_val = new ClassX_Valid(x_debug);
/*	=======================================================================	*/
/// <remarks>
/// Class: ClassX_Form
/// The ClassX_Form object class has the following purpose/function:
///	- supplies client-side properties and methods for 'form' objects
/// </remarks>
/*	-----------------------------------------------------------------------	*/
/// <summary>
/// The ClassX_Group object class is constructed as follows:
///		var thisobj = new ClassX_Form(dbg);
/// - where:
///		- dbg:	debugging switch
/// </summary>
function ClassX_Form(dbg) {
	/*	-------------------------------------------	*/
	/// <remarks>Properties: Public</remarks>
	/*	-------------------------------------------	*/
	this.parameter = "";
	/*	-------------------------------------------	*/
	/// <remarks>Properties: Private</remarks>
	/*	-------------------------------------------	*/
	var _self = this;	// self allows for a method/property to be called internally
	var _debug = (dbg)? dbg : false;
	var _frm;
	/*	-------------------------------------------	*/
	/// <remarks>Region: Accessor methods</remarks>
	/*	-------------------------------------------	*/
	this.GetForm = function() { return(_frm); }
	/*	-------------------------------------------	*/
	/// <remarks>Region: Public methods</remarks>
	/*	-------------------------------------------	*/
	this.Check = _Check;
	this.Submit = _Submit;
	this.Previous = _Previous;
	this.ShowHint = _ShowHint;
	this.Clear = _Clear;
	this.FCKEditor = _FCKEditor;
	/// <summary>Method:_Check</summary>
	function _Check(frm, typ){
		_Debug("_Check:", frm.name, " type:", typ);
		_frm = frm;
		switch (typ) {
			case "login":	if (_ValidateLogin())	_frm.submit();	break;
			case "email":	if (_ValidateEmail())	_frm.submit();	break;
			case "search":	if (_ValidateSearch())	_frm.submit();	break;
			case "select":	_frm.submit();	break;
			case "back":	_Previous();	break;
			case "clear":	_Clear();		break;
			default:		if (typeof(validateForm) != "function")
								_frm.submit(); 	// submit if validate function does not exist
							else if (validateForm(_frm))
								_frm.submit();
							break;
		}
	}
	/// <summary>Method:_Submit</summary>
	// NB: Needs some checking + testing
	function _Submit(frm, url, chk, typ, nme, prc, id, param) {
		_Debug("_Submit:", frm.name, " url:", url);
		_frm = frm;
		var thisurl = url;
		if (typ) {
			thisurl += "&type=" + typ;
			thisurl += "&name=" + nme;
			thisurl += "&process=" + prc;
		}
		if (id)
			thisurl += "&id=" + id;
		if (param)
			thisurl += "&" + param;
		_frm.action = thisurl;
		if (chk)
			_Check(frm, typ);
		else
			_frm.submit();
	}
	/// <summary>Method:_ValidateSearch</summary>
	function _ValidateSearch() {
		return (x_val.Text(_frm.Keywords, "Please enter a search word"));
	}
	/// <summary>Method:_ValidateLogin</summary>
	function _ValidateLogin(){
		if (!x_val.Text(_frm.Usrename, "Please enter your username"))
			return false;
		if (!x_val.Text(_frm.fld_pin, "Please enter your password"))
			return false;
		return true;
	}
	/// <summary>Method:_ValidateEmail</summary>
	function _ValidateEmail(){
		if (!x_val.Text(_frm.Email_To, "Please enter the recipients name"))
			return false;
		if (!x_val.Text(_frm.Email_From, "Please enter your name"))
			return false;
		if (!x_val.Email(_frm.Email_Addr_To, "Please enter the recipients email address"))
			return false;
		if (!x_val.Text(_frm.Email_Subject, "Please enter the subject of the email"))
			return false;
		return true;
	}
	/// <summary>Method:_Previous</summary>
	function _Previous() {
		window.history.back();
	}
	/// <summary>Method:_ShowHint</summary>
	function _ShowHint(hintelem, hinttext) {
		var thishint = document.getElementById(hintelem);
		if (thishint)
			thishint.innerHTML = hinttext;
	}
	/// <summary>Method:_Previous</summary>
	function _Clear(frm) {
		if (frm)
			_frm = frm;
		_frm.reset();
	}
	function _FCKEditor(pth, nme, wid, hgt, tlb, val) {
		_Debug("_FCKEditor", "pth", pth);
		try {
			oFCKeditor = new FCKeditor(nme, wid, hgt, tlb, val);
			oFCKeditor._HTMLEncode = _HtmlEncode;
			oFCKeditor.BasePath = pth + "scripts/FCKeditor/";
			oFCKeditor.Config["CustomConfigurationsPath"] = pth + "fckeditor/fckeditor.js";
			oFCKeditor.Config["StylesXmlPath"] = pth + "fckeditor/fckstyles.xml";
			oFCKeditor.Config["PluginsPath"] = pth + "fckeditor/plugins/";
			//oFCKeditor.Config["EditorAreaCSS"] = pth + "../styles/" + tlb + "/fck_editor.css" ;
			oFCKeditor.Config["EditorAreaCSS"] = pth + "../styles/fck_editor.css" ;
			oFCKeditor.Create();
		} catch (e) {
			alert("FCKeditor Error:"+e.description);
		}
	}
	/// <summary>Method:_HtmlEncode - override to the FCKeditor._HTMLEncode since encoding handled in blogX</summary>
	function _HtmlEncode(text) {
		if ( typeof( text ) != "string" )
			text = text.toString() ;
		return text ;
	}
	/// <summary>Method:_Debug - write debugging message. First param is the method label, followed by name, value pairs</summary>
	function _Debug(arg) {
		if (_debug) {
			s = "<!-- ClassX_Form::"
			s = s.concat(arguments[0], ":");
			var argn = arguments.length;
			for(var i=1; i<argn; i=i+2)
				s = s.concat(arguments[i], ":", arguments[i+1], " ");
			alert(s.concat("-->\n"));
		}
	}
}
/*	=======================================================================	*/
/// <remarks>
/// Class: ClassX_Valid
/// The ClassX_Valid object class has the following purpose/function:
///	- supplies client-side properties and methods for 'form' objects
/// </remarks>
/*	-----------------------------------------------------------------------	*/
/// <summary>
/// The ClassX_Valid object class is constructed as follows:
///		var thisobj = new ClassX_Valid(dbg);
/// - where:
///		- dbg:	debugging switch
/// </summary>
function ClassX_Valid(dbg) {
	/*	-------------------------------------------	*/
	/// <remarks>Properties: Private</remarks>
	/*	-------------------------------------------	*/
	var _self = this;	// self allows for a method/property to be called internally
	var _debug = (dbg)? dbg : false;
	/*	-------------------------------------------	*/
	/// <remarks>Region: Public methods</remarks>
	/*	-------------------------------------------	*/
	this.Text = _Text;
	this.Password = _Password;
	this.Email = _Email;
	this.Check = _Check;
	this.Radio = _Radio;
	this.Drop = _Drop;
	this.Number = _Number;
	this.Cell = _Cell;
	this.IntCell = _IntCell;
	this.DateRange = _DateRange;
	/// <summary>Method:_Text</summary>
	function _Text(thisfld, thishint) {
		if (!thisfld.value) {
			alert(thishint);
			thisfld.focus();
			return false;
			}
		return true;
	}
	/// <summary>Method:_Password</summary>
	function _Password(thisfld, confirmfld, thishint) {
		if (thisfld.value.length < 6) {	//at least 6
			alert(thishint);
			thisfld.focus();
			return (false);
		}
		// check if both password fields are the same
		if (thisfld.value != confirmfld.value) {
			alert("The two passwords are not the same.");
			thisfld.focus();
			return (false);
		}
		return (true);
	}
	/// <summary>Method:_Email</summary>
	function _Email(thisfld, thishint){
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
		strEmail = thisfld.value;
		if (strEmail) {
			// search email text for regular exp matches
			if (strEmail.search(validRegExp) == -1) {
				alert(thishint);
				return false;
			} 
			return true; 
		}
		else alert(thishint);
	}
	/// <summary>Method:_Check</summary>
	function _Check(thisfld, thishint) {
		if (!thisfld.checked) {
			alert(thishint);
			thisfld.focus();	
			return false;
			}
		return true;
	}
	/// <summary>Method:_Radio</summary>
	function _Radio(thisfld, thishint) {
		var bChecked = false;
		for (i=0;i<thisfld.length;i++) {
			if (thisfld[i].checked)
			   return true;
		}
		alert(thishint);
		window.navigate("#" + thisfld.id);
		return false;
	}
	/// <summary>Method:_Text</summary>
	function _Drop(thisfld, thishint) {
		if (thisfld.selectedIndex==0){
			alert(thishint);
			thisfld.focus();	
			return false;
			}
		return true;
	}
	/// <summary>Method:_Text</summary>
	function _Number(thisfld, thishint, checkfld, checktype) {
		var validRegExp = /[0-9]{1,}.{0,}[0-9]{0,}/i;
		var strVal = thisfld.value;
		var isvalid = true;
		if (strVal) {
			if (strVal.search(validRegExp) == -1) 
				isvalid = false;
			else if (checkfld) {
				var thisval = parseFloat(strVal);
				var chckval = parseFloat(checkfld.value);
				if (isNaN(thisval) || isNaN(thisval)) 
					isvalid = false;
				else {
					// use checktype (later..)
					if (thisval < chckval)
						isvalid = false;
				}
			}
		}
		else 
			isvalid = false;
		
		if (isvalid)
			return true;
		else {
			alert(thishint);
			return false;
		}
	}
	/// <summary>Method:_Text</summary>
	/*	==========================================	
		Check for a valid cell number: 				
		-	Check 1: They begin "0[7/8/9][2/3/4]";	
		-	Check 2: only 0-9						
		-	Check 3: must be 10 digits				
		========================================	*/
	function _Cell(thisfld, thishint){
		var retval = (thisfld.value)? 0 : 1;
		if (!retval) {
			var val = thisfld.value;
			var cellexp = /\d{10}/;		// NB this will accept more than 10 digits!
			var strtexp = /0[789][234]/ ;
			if (cellexp.test(val))
				retval = (strtexp.test(val)) ? 0 : 1;
			else
				retval = 1;
		}
		if (retval) {
			alert(thishint);
			thisfld.focus();	
			return false;
		}
		else
			return true;
	}
	/// <summary>Method:_Text</summary>
	function _IntCell(thisfld, thishint){
		var retval = (thisfld.value)? 0 : 1;
		if (!retval) {
			var val = thisfld.value;
			var cellexp = /\d{11}/;		// NB this will accept more than 10 digits!
			var strtexp = /27[789][234]/ ;
			if (cellexp.test(val))
				retval = (strtexp.test(val)) ? 0 : 1;
			else
				retval = 1;
			}
		if (retval) {
			alert(thishint);
			thisfld.focus();	
			return false;
			}
		else
			return true;
	}
	/// <summary>Method:_Text</summary>
	function _DateRange(thisfld, thishint){
		if (_Radio(thisfld, thishint)) {
			var thischk = "";
			var thisid = "";
			for (i=0;i<thisfld.length;i++) {
				if (thisfld[i].checked) {
				   thischk = thisfld[i].value;
				   thisid = thisfld[i].id;
				   break;
			   }
			}
			if (thischk == "Date") {
				var thisdate = document.getElementById(thisid + "_date");
				if (_Drop(thisdate, thishint)) {
					var thismnth = document.getElementById(thisid + "_month");
					return(_Drop(thismnth, thishint));
				}
				else
					return false;
			}
			return true;
		}
		else
			return false;
	}
}
